//
//  HomeViewController.m
//  HuiPinZhiJia
//
//  Created by MS on 16/8/29.
//  Copyright © 2016年 MS. All rights reserved.
//

#import "HomeViewController.h"
#import "SuperNav.h"
#import "CategoryViewController.h"
#import "SDCycleScrollView.h"
#import "HomeCell.h"
#import "DetailViewController.h"
@interface HomeViewController ()<UITextFieldDelegate,SDCycleScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UIView *searchView;
@property (nonatomic,strong)UIView *headerView;
@property (nonatomic,strong)UITextField *textField;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,strong)DetailViewController *dvc;
@end

@implementation HomeViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"首页";
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.41 green:0.79 blue:1.00 alpha:1.00]];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],UITextAttributeFont : [UIFont boldSystemFontOfSize:18]}];
    
    _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, (screenHeight- 66)*4/10)];
    
    [self addSearchView];
    
    [self addScrollView];
    
    [self createTableView];
    
    // Do any additional setup after loading the view.
}

#pragma mark CreateTableView
- (void)createTableView
{
    _dataSource = [[NSMutableArray alloc]init];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, screenWidth, screenHeight - 66) style:UITableViewStylePlain];
    [_tableView registerNib:[UINib nibWithNibName:@"HomeCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = _headerView;
    
    [self.view addSubview:_tableView];
}

#pragma mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cell";
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
        return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (screenHeight- 66)*6/10/4;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SLLog(@"你选中了第%ld行",indexPath.row);
     _dvc = [[DetailViewController alloc]init];
    
    [self.navigationController pushViewController:_dvc animated:YES];
}

#pragma mark 搜索
- (void)addSearchView
{
    _searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 44)];
    _searchView.backgroundColor = [UIColor colorWithRed:190/255.0 green:190/255.0 blue:190/255.0 alpha:.7];
    
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(45, 2, screenWidth - 45, 40)];
    [_searchView addSubview:_textField];
    _textField.placeholder = @"请输入搜索关键字";
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(5, 0, 30, 34)];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fangdajing.png"]];
    imageView.frame = CGRectMake(1, 1, 28, 28);
    [_textField.leftView addSubview:imageView];
    _textField.leftViewMode = UITextFieldViewModeAlways;
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textField.returnKeyType = UIReturnKeySearch;
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(1, 1, 38, 38);
    [btn setBackgroundImage:[UIImage imageNamed:@"caidan.png"] forState:UIControlStateNormal];
    btn.tag = 101;
    [btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
    [_searchView addSubview:btn];
    
    [_headerView addSubview:_searchView];

}

- (void)btn:(id)button
{
    self.tabBarController.selectedIndex = 1;
}

#pragma mark 轮播
- (void)addScrollView
{
    UIScrollView *demoContainerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, screenWidth, (screenHeight- 66)*3/10)];
    demoContainerView.contentSize = CGSizeMake(self.view.frame.size.width, 180);
    
    NSArray *imagesURLStrings = @[
                                  @"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a4b3d7085dee3d6d2293d48b252b5910/0e2442a7d933c89524cd5cd4d51373f0830200ea.jpg",
                                  @"https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a41eb338dd33c895a62bcb3bb72e47c2/5fdf8db1cb134954a2192ccb524e9258d1094a1e.jpg",
                                  @"http://c.hiphotos.baidu.com/image/w%3D400/sign=c2318ff84334970a4773112fa5c8d1c0/b7fd5266d0160924c1fae5ccd60735fae7cd340d.jpg"
                                  ];
    SDCycleScrollView *cycleScroll = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0,screenWidth, 180) delegate:self placeholderImage:[UIImage imageNamed:@"caidan.img"]];
    cycleScroll.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    cycleScroll.currentPageDotColor = [UIColor whiteColor];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cycleScroll.imageURLStringsGroup = imagesURLStrings;
    });
    [demoContainerView addSubview:cycleScroll];

    [_headerView addSubview:demoContainerView];

}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);
    
    [self.navigationController pushViewController:[NSClassFromString(@"DemoVCWithXib") new] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
